package com.rocket.bedroom.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rocket.bedroom.pojo.FloorEntity;
import com.rocket.bedroom.pojo.RoomEntity;
import com.rocket.bedroom.pojo.custom.RoomCustomEntity;
import com.rocket.bedroom.pojo.param.RoomParam;
import com.rocket.bedroom.service.IRoomService;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 周毅
 * @ClassName:
 * @Description: TODO
 * @Date: Create in 14:52 2020/5/15
 * @ModifyBy:
 */
@RestController
@RequestMapping(value = "/room")
public class RoomController {

    @Resource
    private IRoomService roomService;

    @GetMapping(value = "/pageRoom")
    public PageInfo<RoomCustomEntity> pageRoom(RoomParam roomParam, Integer pageNumber, Integer pageSize) {
        if (null == pageNumber || 0 >= pageNumber) {
            pageNumber = 1;
        }
        if (null == pageSize || 0 >= pageSize) {
            pageSize = 10;
        }
        PageHelper.startPage(pageNumber, pageSize);
        List<RoomCustomEntity> roomCustomList = roomService.selectRoomCustom(roomParam);
        return new PageInfo<>(roomCustomList);
    }

    @PostMapping(value = "/addRoomOpenUp")
    public int addRoomOpenUp(RoomEntity roomEntity, Integer berthNumber) {
        return roomService.addRoomAndBerth(roomEntity, berthNumber) ? 1 : -1;
    }

    @GetMapping(value = "/findRoomById/{roomId}")
    public Map<String, Object> findRoomById(@PathVariable("roomId") Long roomId) {
        //加载修改页面时，响应内容添加床位
        Map<String, Object> map = new HashMap<>();
        Assert.notNull(roomId, "find Room Id IS NULL");
        RoomEntity roomById = roomService.getById(roomId);
        map.put("room", roomById);
        return map;
    }

    @PutMapping(value = "/modifyRoomById")
    public int modifyRoomById(RoomEntity roomEntity) {
        Assert.notNull(roomEntity, "ADD Room IS NULL");
        Assert.notNull(roomEntity.getRoomName(), "ADD Room RoomName IS NULL");
        boolean updateRoom = roomService.updateById(roomEntity);
        return updateRoom ? 1 : -1;
    }

}
